﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using static System.Net.Mime.MediaTypeNames;

namespace BaseDll
{
    /// <summary>
    /// 发送服务
    /// </summary>
    public class SendServer
    {
        /// <summary>
        /// 服务
        /// </summary>
        public List<UdpClient> server { get; set; }

        /// <summary>
        /// tcp
        /// </summary>
        public TcpListener commServer { get; set; }


        public ushort port { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public int serverCount { get; set; }
        /// <summary>
        /// 链接用户
        /// </summary>
        List<nint> linkUser { get; set; }
        /// <summary>
        /// 用户变化
        /// </summary>

        public event Action<Dictionary<nint, string>> userChaneg;
        public event Action<int, MemoryStream> dataChange;
        /// <summary>
        /// tcp数据
        /// </summary>
        public event Action<TcpClient, string> TcpData;

        public SendServer()
        {
            linkUser = new List<nint>();
            //Console.Title = "发送";
        }

        public void Start()
        {
            //var ip = IPAddress.Parse("127.0.0.1");
            commServer = new(IPAddress.Loopback, port);
            commServer.Start();
            server = new();
            for (int i = 1; i <= serverCount; i++)
            {
                // 创建udp
                var udp = new UdpClient();
                server.Add(udp);
            }
            Task.Factory.StartNew(Receive);

        }
        // 监听客户端发来的请求
        IPAddress remoteIP = null;
        private void Receive()
        {
            var client = commServer.AcceptTcpClient();
            remoteIP = ((IPEndPoint)client.Client.RemoteEndPoint).Address;


            Task.Factory.StartNew(new Action(() =>
            {
                TcpRead(client);
            }));
        }

        private void TcpRead(TcpClient tcp)
        {
            while (true)
            {
                byte[] bytes = new byte[1024];
                NetworkStream data = tcp.GetStream();
                data.Read(bytes, 0, bytes.Length);
                var dstdata = UTF8Encoding.UTF8.GetString(bytes, 0, bytes.Length);
                Console.WriteLine(dstdata);
                TcpData?.Invoke(tcp, dstdata);


                Thread.Sleep(200);
            }
        }

        /// <summary>
        /// 发送
        /// </summary>
        /// <param name="connId"></param>
        /// <param name="data"></param>
        public void Send(int index, byte[] data)
        {
            Console.WriteLine("发送大小:" + data.Length);
            server[index].Send(data);
        }

        public void Close()
        {
            server.ForEach(x => x.Close());
            commServer.Stop();
        }

        public void TcpSend(TcpClient tcp, string data)
        {
            var databyte = UTF8Encoding.UTF8.GetBytes(data);
            tcp.Client.Send(databyte);
        }
    }
}
